import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { ReuseTabService } from '@delon/abc';
import { NzMessageService } from 'ng-zorro-antd';
import { LocalStorage } from '@core/local.storage';
import { ClearLoginService } from '@core/clearLogin.service';
import { _HttpClient } from '@delon/theme';
import { HttpParams, HttpHeaders } from '@angular/common/http';
import { environment } from '../../../../environments/environment';
const responseHeaders = new HttpHeaders().set('Content-Type', 'application/json;charset=UTF-8');

@Component({
    selector: 'passport-login',
    templateUrl: './login.component.html',
    styleUrls: ['./login.component.less']
})

export class UserLoginComponent implements OnInit {
    form: FormGroup;
    error = '';
    loading = false;
    loginId = '';
    loginPwd = '';
    title = '';
    copyright = '';
    areaInfo = '';
    loginRes: any;
    menuJson: any;

    constructor(
        fb: FormBuilder,
        private http: _HttpClient,
        private router: Router,
        public ls: LocalStorage,
        private reuseTabService: ReuseTabService,
        public msg: NzMessageService,
        private clearLoginService: ClearLoginService
    ) {
        this.form = fb.group({
            userName: [null, Validators.required],
            password: [null, Validators.required]
        });
    }

    get userName() {
        return this.form.controls.userName;
    }
    get password() {
        return this.form.controls.password;
    }

    ngOnInit() { 
        this.clearLoginService.clearAll();
        if (JSON.stringify(this.ls.get('areaInfo')) === 'false') {
            this.router.navigate(['main']);
            return;
        }
        // 按地区发布设置
        this.areaAction().subscribe((res: any) => {
            this.areaInfo = res.type;
            this.ls.set('areaInfo', res.type);
            this.areaSet('login', (resTitle, resCopyright) => {
                resTitle = resTitle + '电务段';
                this.ls.set('areaSet', resTitle);
                this.title = resTitle;
                this.copyright = resCopyright;
            });
        });
    }
    areaSet(name, callback) {
        switch (name) {
            case 'index':
                callback('2', './assets/index/kunming_index1.png');
                break;
            case 'header':
                callback('./assets/kunming_logo.png');
                break;
            case 'login':
                callback('昆明南', './assets/login/kunming_copyright.png', './assets/control/kunming_headlogo.png');
                break;
        }
    }
    areaAction() {
        const url = environment.apiBase + '/csmis/publicpro/getduan.action';
        return this.http.get(url);
    }

    // 登录
    loginAction(id, pwd) {
        const url = environment.apiBase + '/csmis/login/login.jwt';
        const params = new HttpParams()
            .set('vcNumber', id)
            .set('vcPassWord', pwd);

        return this.http.post(url, params, { headers: responseHeaders }, { responseType: 'text' });
    }

    submit() {
        if (this.loginId === '' || this.loginPwd === '') {
            this.msg.error('用户名或密码不能为空', { nzDuration: 1000 });
            return false;
        }
        this.error = '';
        this.loading = true;
        this.loginAction(this.loginId, this.loginPwd).subscribe((res: any) => {
            if (typeof (res) !== 'object') {
                res = JSON.parse(res.replace(/\'/g, '"'));
            }
            if (res.type === 'success') {
                this.reuseTabService.clear();
                this.ls.set('loginInfo', JSON.stringify(res.response.data));
                this.loginSkip();
            } else {
                this.msg.error(res.message, { nzDuration: 1000 });
                this.clearLoginService.clearAll();
                this.loginId = '';
                this.loginPwd = '';
                this.loading = false;
            }
        }, err => {
            this.msg.error('接口错误');
            this.loading = false;
        });
    }

    loginSkip() { 
        switch (this.areaInfo) {
            case 'KMBUR_KMNSEG':
                this.router.navigate(['indexkmn']);
                break;
            case 'WLMQBUR_HAMSEG':
                this.router.navigate(['control']);
                break;
            default:
                this.router.navigate(['main']);
                this.ls.set('menuTree', 'all');
                break;
        }
    }
}
